<?php
    include('checkValidSession.php');

    echo $_FILES['fileToUpload']['tmp_name'];
    echo '<br>';
    if(!is_uploaded_file($_FILES['fileToUpload']['tmp_name']))
    {
        $_SESSION['message'] = 'An error occured. No file is uploaded.';
        header('Location: uploadForm.php');
        exit;
    }

    if(!isset($_FILES) or $_FILES['fileToUpload']['error'] > 0 or $_FILES['fileToUpload']['size'] == 0)
    {
        $_SESSION['message'] = 'An error occured. The file is uploaded.';
        header('Location: uploadForm.php');
        exit;
    }

    if(!file_exists($_SESSION['username'].DIRECTORY_SEPARATOR))
    {
        mkdir($_SESSION['username'].DIRECTORY_SEPARATOR);
    }

    $uploadAt = $_SESSION['username'].DIRECTORY_SEPARATOR.basename($_FILES['fileToUpload']['name']);
    if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadAt))
    {
        $_SESSION['message'] = 'The file was successfully uploaded.';
    }
    else
    {
        $_SESSION['message'] = 'The file coulld not be uploaded to the correct directory';
    }
    header('Location: uploadForm.php');
?>
